package com.campusflea;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.campusflea.DateBase.UserInfo;
import com.campusflea.custom.ClearWriteEditText;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SQLQueryListener;

public class LoginActivity extends AppCompatActivity {
    private EditText admin;
    private EditText password;

    private Button login;
    private String str_admin;
    private String str_password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        admin =(ClearWriteEditText)findViewById(R.id.de_login_phone);
        password =(ClearWriteEditText)findViewById(R.id.de_login_password);
        login = (Button)super.findViewById(R.id.de_login_sign);
        TextView b2 = (TextView)super.findViewById(R.id.de_login_register);
        TextView b3 = (TextView)super.findViewById(R.id.de_login_forgot);
        login.setOnClickListener(lisenter);
        b2.setOnClickListener(lisenter1);
        b3.setOnClickListener(lisenter1);
        Bmob.initialize(this,"36b62198745ed1900e5f1e8d55eb660d");
        admin.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() >11) {
                    Toast.makeText(getApplicationContext(), "手机号格式错误！", Toast.LENGTH_LONG).show();;
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
    private View.OnClickListener lisenter = new View.OnClickListener() {
        public void onClick(View v) {
            Button btn =(Button)v;
            str_admin =admin.getText().toString().trim();
            str_password =password.getText().toString().trim();
            try{
                switch(btn.getId())
                {
                    //登录验证
                    case R.id.de_login_sign:
                    {

                        if(str_admin.equals("")||str_password.equals("")){
                            Toast.makeText(getApplicationContext(), "账号密码不能为空！", Toast.LENGTH_LONG).show();;
                        }
                        else {
                            String bql ="select * from UserInfo where phone =? and password =?";
                            BmobQuery<UserInfo> query=new BmobQuery<UserInfo>();
                            query.setSQL(bql);
                            query.setPreparedParams(new Object[]{str_admin,str_password});
                            query.doSQLQuery(new SQLQueryListener<UserInfo>(){
                                @Override
                                public void done(BmobQueryResult<UserInfo> bmobQueryResult, BmobException e) {
                                    List<UserInfo> list = (List<UserInfo>) bmobQueryResult.getResults();
                                    if(list.size()==0){
                                        Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_LONG).show();
                                    }
                                    else {
                                        Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show();
                                        Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                                        intent.putExtra("phone",str_admin);
                                        startActivity(intent);
                                    }
                                }
                            });
                        }
                        break;
                    }
                }
            }catch(Exception e){}
        }
    };
    private View.OnClickListener lisenter1 = new View.OnClickListener() {
        public void onClick(View v) {

            TextView tv =(TextView)v;
            try{

                switch(tv.getId())
                {
                    case R.id.de_login_register: //跳转代码
                    {
                        // Toast.makeText(getApplicationContext(), "用户名或密码出错！", Toast.LENGTH_LONG).show();
                        Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                        startActivity(intent);
                        break;
                    }
                    case R.id.de_login_forgot: //跳转代码
                    {
                        // Toast.makeText(getApplicationContext(), "用户名或密码出错！", Toast.LENGTH_LONG).show();
                        Intent intent=new Intent(LoginActivity.this,ForgetPasswordActivity.class);
                        startActivity(intent);
                        break;
                    }
                }
            }catch(Exception e){}
        }
    };

}
